package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsReagentForm;
import com.ruoyi.project.product.domain.InsReagentFormDetail;
import com.ruoyi.project.product.domain.InsReagentFormDetailDTO;
import com.ruoyi.project.product.service.IInsReagentFormDetailService;
import com.ruoyi.project.product.service.IInsReagentFormService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 试剂表单详细Controller
 *
 * @author taney
 * @date 2020-11-17
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/reagentFormDetail")
public class InsReagentFormDetailController extends BaseController {

    private final IInsReagentFormDetailService iInsReagentFormDetailService;
    private final IInsReagentFormService iInsReagentFormService;


    /**
     * 查询试剂表单详细列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentFormDetail insReagentFormDetail) {
        startPage();
        LambdaQueryWrapper<InsReagentFormDetail> lqw = new LambdaQueryWrapper<InsReagentFormDetail>();
        if (insReagentFormDetail.getReagentFormId() != null) {
            lqw.eq(InsReagentFormDetail::getReagentFormId, insReagentFormDetail.getReagentFormId());
        }
        if (StringUtils.isNotBlank(insReagentFormDetail.getReagentType())) {
            lqw.eq(InsReagentFormDetail::getReagentType, insReagentFormDetail.getReagentType());
        }
        if (insReagentFormDetail.getBottleTypeId() != null) {
            lqw.eq(InsReagentFormDetail::getBottleTypeId, insReagentFormDetail.getBottleTypeId());
        }
        if (insReagentFormDetail.getReagentVolume() != null) {
            lqw.eq(InsReagentFormDetail::getReagentVolume, insReagentFormDetail.getReagentVolume());
        }
        if (insReagentFormDetail.getReagentSize() != null) {
            lqw.eq(InsReagentFormDetail::getReagentSize, insReagentFormDetail.getReagentSize());
        }
        List<InsReagentFormDetail> list = iInsReagentFormDetailService.list(lqw);
        List<InsReagentFormDetailDTO> insReagentFormDetailDTOS = new ArrayList<>();
        list.forEach(item -> {
            InsReagentFormDetailDTO insReagentFormDetailDTO = new InsReagentFormDetailDTO();
            BeanUtils.copyBeanProp(insReagentFormDetailDTO, item);
            InsReagentForm insReagentForm = iInsReagentFormService.getById(item.getReagentFormId());
            insReagentFormDetailDTO.setInsReagentForm(insReagentForm);
            insReagentFormDetailDTOS.add(insReagentFormDetailDTO);
        });
        return getDataTable(insReagentFormDetailDTOS);
    }

    /**
     * 导出试剂表单详细列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:export')")
    @Log(title = "试剂表单详细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentFormDetail insReagentFormDetail) {
        LambdaQueryWrapper<InsReagentFormDetail> lqw = new LambdaQueryWrapper<InsReagentFormDetail>(insReagentFormDetail);
        List<InsReagentFormDetail> list = iInsReagentFormDetailService.list(lqw);
        ExcelUtil<InsReagentFormDetail> util = new ExcelUtil<InsReagentFormDetail>(InsReagentFormDetail.class);
        return util.exportExcel(list, "reagentFormDetail");
    }

    /**
     * 获取试剂表单详细详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:query')")
    @GetMapping(value = "/{bottleDetailId}")
    public AjaxResult getInfo(@PathVariable("bottleDetailId") Long bottleDetailId) {
        InsReagentFormDetail item = iInsReagentFormDetailService.getById(bottleDetailId);
        InsReagentFormDetailDTO insReagentFormDetailDTO = new InsReagentFormDetailDTO();
        BeanUtils.copyBeanProp(insReagentFormDetailDTO, item);
        InsReagentForm insReagentForm = iInsReagentFormService.getById(item.getReagentFormId());
        insReagentFormDetailDTO.setInsReagentForm(insReagentForm);
        return AjaxResult.success(insReagentFormDetailDTO);
    }

    /**
     * 新增试剂表单详细
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:add')")
    @Log(title = "试剂表单详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentFormDetail insReagentFormDetail) {
        return toAjax(iInsReagentFormDetailService.save(insReagentFormDetail) ? 1 : 0);
    }

    /**
     * 修改试剂表单详细
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:edit')")
    @Log(title = "试剂表单详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentFormDetail insReagentFormDetail) {
        return toAjax(iInsReagentFormDetailService.updateById(insReagentFormDetail) ? 1 : 0);
    }

    /**
     * 删除试剂表单详细
     */
    @PreAuthorize("@ss.hasPermi('product:reagentFormDetail:remove')")
    @Log(title = "试剂表单详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bottleDetailIds}")
    public AjaxResult remove(@PathVariable Long[] bottleDetailIds) {
        return toAjax(iInsReagentFormDetailService.removeByIds(Arrays.asList(bottleDetailIds)) ? 1 : 0);
    }
}
